package com.example.q_kang.hhh.setting;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.view.SelectPictureDialog;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.base.MVPActivity;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.ImageUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class BindingWechatActivity extends MVPActivity<BindingWechatPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_wechat)
    EditText etWechat;
    @BindView(R.id.iv_QRcode)
    ImageView ivQRcode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private String imgUrl;
    private SelectPictureDialog selectPictureDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_binding_wechat;
    }

    @Override
    public BindingWechatPresenter getPresenter() {
        return new BindingWechatPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, tvGetCode, view -> mPresenter.sendCode((String) SPUtils.get(App.INSTANCE, SpConfig.MOBILE, "")));
        ClickUtil.click(300, ivQRcode, view -> selectPic());
        ClickUtil.click(300, btnSubmit, view -> {
            if (TextUtils.isEmpty(etName.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBindingWechatActivity2));
                return;
            }
            if (TextUtils.isEmpty(etWechat.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBindingWechatActivity3));
                return;
            }
            if (TextUtils.isEmpty(imgUrl)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBindingWechatActivity4));
                return;
            }
            if (TextUtils.isEmpty(etCode.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBindingWechatActivity5));
                return;
            }
            mPresenter.bindWeChat(etName.getText().toString(),
                    etWechat.getText().toString(),
                    imgUrl,
                    etCode.getText().toString());
        });
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BindingWechatActivity.class);
        context.startActivity(intent);
    }

    public void uploadImgSuccess(String src) {
        this.imgUrl = src;
        ImageUtil.setImage(this, imgUrl, ivQRcode);
    }

    public void bindWeChatSuccess() {
        finish();
    }

    public void sendCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(BindingWechatActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(BindingWechatActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            String mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadImg(new File(mImagePath));
        }
    }
}
